#coding:utf-8
__author__ = 'hl'
import  wx
import grid.gridui as grid
import uidata.packageuidata as packageuidata
import subject.notifier as notifier
import ui.addChannelUI as addChannelUI
import os
import os.path
import packaging.cmdPackaingAndHavingApk as dospackaging
import utils.operateCache as operateCache



class PackagingUI(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self,parent,id)
        cache = operateCache.OperateCache('./packageuiCache.txt','r')
        self.uiData = cache.Get()
        cache.closeFile()
        if not self.uiData:
            self.uiData = packageuidata.PackageUIData('','',[u'渠道名称',u'渠道编码'],[])

        ui = self.CreateUI()
        self.SetSizer(ui)
        ui.Fit(parent)
        ui.SetSizeHints(parent)

        notfyRawAppPath = notifier.Notifier()
        notfyRawAppPath.addObserver(self.rawAppPath.SetValue)
        self.uiData.bindSubjectForRwaAppPath(notfyRawAppPath)

        notfySaveAppsPath = notifier.Notifier()
        notfySaveAppsPath.addObserver(self.svPathAppPackaged.SetValue)
        self.uiData.bindSubjectForSaveAppsPath(notfySaveAppsPath)

        notfyAddingData = notifier.Notifier()
        notfyAddingData.addObserver(self.grid.AppendRows)
        self.uiData.bindSubjectForAddingData(notfyAddingData)

        notfyUpdatingData = notifier.Notifier()
        notfyUpdatingData.addObserver(self.grid.Update)
        self.uiData.bindSbjectForUpdateGridDatas(notfyUpdatingData)






    def CreateUI(self):
        #创建界面标题
        titleLbl = wx.StaticText(self,-1,u'多渠道打包工具')

        #原始包路径
        self.rawAppPath = wx.TextCtrl(self,-1,self.uiData.getRwaAppPath(),size=(200,20))
        chsPath = wx.Button(self,-1,u'样包路径')

        #打包后存放路径
        self.svPathAppPackaged = wx.TextCtrl(self,-1,self.uiData.getSaveAppsPath(),size=(200,20))
        chsSvPath = wx.Button(self,-1,u'保存路径')

        #渠道包名称列表
        self.grid = grid.GridUI(self,-1,self.uiData.getGridDatas(),self.uiData.getGridLbls(),(280,200))

        #录入渠道名
        svnChanels = wx.Button(self,-1,u'添加渠道')

        #打包
        package = wx.Button(self,-1,u'打包')

        #日志
        self.log = wx.TextCtrl(self,-1,u'',size = (280,100),style=wx.TE_MULTILINE)

        #布局
        mainsizer = wx.BoxSizer(wx.VERTICAL)

        #标题
        mainsizer.Add(titleLbl,0,wx.CENTER|wx.TOP|wx.BOTTOM,15)
        mainsizer.Add(wx.StaticLine(self,-1),0,wx.EXPAND|wx.ALL,5)

        #原始包路径
        subsizer = wx.FlexGridSizer(1,0)
        subsizer.Add(self.rawAppPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP,10)
        subsizer.Add(chsPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.LEFT,10)
        mainsizer.Add(subsizer,0,wx.EXPAND|wx.LEFT|wx.RIGHT,10)

        #打包后存放路径
        subsizer = wx.FlexGridSizer(1,0)
        subsizer.Add(self.svPathAppPackaged,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP,10)
        subsizer.Add(chsSvPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.LEFT,10)
        mainsizer.Add(subsizer,0,wx.EXPAND|wx.LEFT|wx.RIGHT,10)

        #列表
        subsizer = wx.GridBagSizer(5,5)
        subsizer.Add(self.grid,(1,0),(4,4))
        mainsizer.Add(subsizer)

        #操作
        subsizer = wx.GridBagSizer(5,5)
        subsizer.Add(svnChanels,(0,3),(2,1))
        subsizer.Add(package,(0,7),(2,1))
        mainsizer.Add(subsizer)

        #日志
        mainsizer.Add(self.log,1,wx.EXPAND|wx.ALL,10)

        #绑定事件
        chsPath.Bind(wx.EVT_BUTTON,self.OnChsRawApkPath)
        chsSvPath.Bind(wx.EVT_BUTTON,self.OnChsSaveApkPackaged)
        svnChanels.Bind(wx.EVT_BUTTON,self.OnAddChannel)
        package.Bind(wx.EVT_BUTTON,self.OnPackaing)
        self.svPathAppPackaged.Bind(wx.EVT_TEXT,self.OnSvPathAppPackaged)
        self.rawAppPath.Bind(wx.EVT_TEXT,self.OnRawAppPath)
        return mainsizer

    def OnChsRawApkPath(self,evt):
        fileDialog = wx.FileDialog(self,u'选择样包')
        fileDialog.ShowModal()
        self.uiData.setRwaAppPath(fileDialog.GetPath())

    def OnChsSaveApkPackaged(self,evt):
        fileDialog = wx.DirDialog(self,u'保存路径')
        fileDialog.ShowModal()
        self.uiData.setSaveAppsPath(fileDialog.GetPath())

    def SaveCache(self):
        #删除第一列
        datas = self.grid.table.datas

        if datas:
            for index in range(len(datas)):
                del datas[index][0]
        self.uiData.setGridDatas(datas)
        del self.uiData.getGridLbls()[0]
        self.uiData.bindSbjectForUpdateGridDatas(None)
        self.uiData.bindSubjectForAddingData(None)
        self.uiData.bindSubjectForSaveAppsPath(None)
        self.uiData.bindSubjectForRwaAppPath(None)
        cache = operateCache.OperateCache('./packageuiCache.txt','w')
        cache.Save(self.uiData)
        cache.closeFile()


    def OnAddChannel(self,evt):
        addchlui = addChannelUI.AddChannelUI(self,-1)
        addchlui.ShowModal()

    def OnPackaing(self,evt):
        if not self.uiData.getRwaAppPath():
            print u'未现在样包'
            return
        if not self.uiData.getSaveAppsPath():
            print u'渠道包会放到工具根目录apks文件夹中'
            if not os.path.exists('./apks/'):
                os.mkdir('./apks')
            self.uiData.setSaveAppsPath('./apks')
        channels = self.grid.GetSelectedRowS()
        if not len(channels):
            print u'未选择渠道平台'
            return

        dospackaging.packaging(self.uiData.getRwaAppPath(),self.uiData.getSaveAppsPath(),channels)

    def OnSvPathAppPackaged(self,evt):
        self.uiData.setSaveAppsPathOnly(self.svPathAppPackaged.GetValue())

    def OnRawAppPath(self,evt):
        self.uiData.setRwaAppPathOnly(self.rawAppPath.GetValue())